<?php

namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);
use app\admin\Controller;
use think\Db;
use think\Request;
use think\Validate;

class Platform extends Controller
{
    use \app\admin\traits\controller\Controller;
    //
    public function index(Request $request)
    {
        if ($request->isPost()){

        }else{
            $data = input();
            if ($data['name']){
                $map['name'] = ['like','%'.$data['name'].'%'];
            }
            if ($data['platform_name']){
                $map['platform_name'] = ['like','%'.$data['platform_name'].'%'];
            }
            $platform = Db::name('platform')->where('isdelete',0)->where($map)->order('id','asc')->paginate(20);
            $total = $platform->total();
            return view('index',compact('platform','total'));
        }
    }

    /**
     * 添加平台
     * @url /admin/platform/add
     * @param Request $request
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function add(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();//添加平台数据
            //验证数据
            $rules = [
                'name'=>'require|unique:platform',
                'platform_name'=>'require|alphaDash|unique:platform',
            ];
            $msg = [
                'name.require'=>'平台名称不能为空',
                'name.unique'=>'平台名称不能重复',
                'platform_name.require'=>'平台简称不能为空',
                'platform_name.alphaDash'=>'平台简称必须是字母或数字或下划线',
                'platform_name.unique'=>'平台简称不能重复',
            ];
            $validate = new Validate($rules,$msg);
            $result = $validate->check($postData);
            if (!$result){
                return ajax_return_adv_error($validate->getError());
            }
            //添加时间
            $postData['create_time'] = time();
            $addPlatform = Db::name('platform')->strict(false)->insertGetId($postData);//添加平台
            if ($addPlatform){
                return ajax_return_adv('添加平台成功');
            }else{
                return ajax_return_adv_error('添加平台失败');
            }
        }else{
            return view('edit');
        }
    }

    public function edit(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();//添加平台数据
            //验证数据
            $rules = [
                'name'=>'require|unique:platform',
                'platform_name'=>'require|alphaDash|unique:platform',
                'id'=>'require',
            ];
            $msg = [
                'name.require'=>'平台名称不能为空',
                'id.require'=>'id不能为空',
                'name.unique'=>'平台名称不能重复',
                'platform_name.require'=>'平台简称不能为空',
                'platform_name.alphaDash'=>'平台简称必须是字母或数字或下划线',
                'platform_name.unique'=>'平台简称不能重复',
            ];
            $validate = new Validate($rules,$msg);
            $result = $validate->check($postData);
            if (!$result){
                return ajax_return_adv_error($validate->getError());
            }
            //查找数据
            $platform = Db::name('platform')->where('id',$postData['id'])->find();
            if (!$platform){
                return ajax_return_adv_error('不存在该平台信息');
            }
           $up =  Db::name('platform')->where('id',$postData['id'])
               ->update(['name'=>$postData['name'],'platform_name'=>$postData['platform_name']]);
            if ($up){
                return ajax_return_adv('更新成功');
            }else{
                return ajax_return_adv_error('更新失败','','','','','',$postData['id']);
            }
        }else{
            $id = input('id');
            if (!$id){
                return ajax_return_adv_error('缺少参数');
            }
           $platform =  Db::name('platform')->where('id',$id)->find();
            if (!$platform){
                return ajax_return_adv_error('不存在该平台信息');
            }
            return view('',compact('platform','id'));
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id = input('id');
        $id = explode(',',$id);
        foreach ($id as $k=>$v){
            $up = Db::name('platform')->where('id',$v)->update(['isdelete'=>1]);
        }
        if ($up){
            return ajax_return_adv('移到回收站成功');
        }else{
            return ajax_return_adv_error('移到回收站失败');
        }
    }

    /**
     * 查询移到回收站的数据
     *
     */
    public function recyclebin()
    {
        $data = input();

        if ($data['name']){
            $map['name'] = ['like','%'.$data['name'].'%'];
        }
        if ($data['platform_name']){
            $map['platform_name'] = ['like','%'.$data['platform_name'].'%'];
        }
        $platform = Db::name('platform')->where('isdelete',1)->where($map)->paginate(20);
        $total = $platform->total();
        $hui = 1;
        return view('index',compact('platform','total','hui'));
    }

    /**
     * 从回收站恢复数据
     *
     */
    public function recycle()
    {
        $id = input('id');
        $id = explode(',',$id);
        foreach ($id as $k=>$v){
            $up = Db::name('platform')->where('id',$v)->update(['isdelete'=>0]);
        }
        if ($up){
            return ajax_return_adv('恢复成功!');
        }else{
            return ajax_return_adv_error('恢复失败!');
        }
    }

    /**
     * 彻底删除数据
     *
     */
    public function deleteforever()
    {
        $id = input('id');
        $id = explode(',',$id);
        foreach ($id as $k=>$v){
            $up = Db::name('platform')->delete($id);
        }
        if ($up){
            return ajax_return_adv('删除数据成功!');
        }else{
            return ajax_return_adv_error('删除数据失败!');
        }
    }

    /**
     * 清空回收站
     */
    public function clear()
    {
        $del = Db::name('platform')->where('isdelete',1)->delete();
        if ($del){
            return ajax_return_adv('清空回收站成功!');
        }else{
            return ajax_return_adv_error('清空回收站失败');
        }
    }

}
